package com.d404.servershow;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.mina.core.session.IoSession;

import im.notify.server.UserInfoAttached;
import manager.DefaultSessionManager;

public class ShowOnline extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		StringBuffer sb = new StringBuffer();
		sb.append("账号，终端类型，终端型号，系统型号，登录时间\n");
		for(IoSession ios:DefaultSessionManager.share().getSessions())
		{
			UserInfoAttached uia = (UserInfoAttached) ios.getAttribute("userInfo");
			sb.append(uia.getUserAccount()).append(",").
				append(uia.getChannel()+",").append(uia.getDeviceModel()+",").
				append(uia.getOsVersion()+",").append((System.currentTimeMillis()-uia.getBindTime())/1000).append("\n");
		}
		
		resp.getWriter().print(sb.toString());
	}
}
